package com.vgemv.jsutilitysdkdemo.module.Menu.Share;

import android.content.Context;
import android.graphics.Rect;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RelativeLayout;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.RecyclerView;

import com.vgemv.jsutilitysdk.Display.JsActivityContentDisplayManager.JsActivityContentDisplayManager;
import com.vgemv.jsutilitysdk.Display.JsActivityContentDisplayManager.JsDisplayCallback;
import com.vgemv.jsutilitysdk.Display.JsActivityContentDisplayManager.JsDisplayStyle;
import com.vgemv.jsutilitysdk.Display.JsViewDisplayManager.JsDisplayAnimationType;
import com.vgemv.jsutilitysdk.Display.JsViewDisplayManager.JsUndisplayAnimationType;
import com.vgemv.jsutilitysdk.geometry.JsGeometryManager;
import com.vgemv.jsutilitysdk.log.JsLogManager;
import com.vgemv.jsutilitysdk.Menu.JsMenuView.JsMenuItemView.JsMenuItemView;
import com.vgemv.jsutilitysdk.Menu.JsMenuView.JsMenuItemVm.JsMenuItemVm;
import com.vgemv.jsutilitysdk.Menu.JsMenuView.JsMenuView;
import com.vgemv.jsutilitysdk.Menu.JsMenuView.JsMenuViewAdapter;
import com.vgemv.jsutilitysdk.Menu.JsMenuView.JsMenuViewBasicCallback;
import com.vgemv.jsutilitysdk.Menu.JsMenuView.JsMenuViewVm;
import com.vgemv.jsutilitysdkdemo.module.Menu.MenuActivityVm;
import com.vgemv.jsutilitysdkdemo.R;

import java.util.ArrayList;

//setupStaticUi
//setupDynamicUi

public class MenuViewActivity extends AppCompatActivity {

    private RelativeLayout mRelativeLayout;
    private MenuActivityVm mvm = new MenuActivityVm();
    private boolean mb_isSharing = false;
    private JsMenuView mMenuView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mRelativeLayout = new RelativeLayout(this);
        mRelativeLayout.setId(View.generateViewId());
        setContentView(mRelativeLayout);
        setupStaticUi();
        updateUi(mvm);
    }

    void setupStaticUi() {
        Button btn_share = new Button(this);
        //内容
        btn_share.setText("Share!!!");
        //布局
        int btnSize = JsGeometryManager.pixelFromDp(100);
        RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(btnSize,btnSize);
        lp.addRule(RelativeLayout.CENTER_IN_PARENT);
        mRelativeLayout.addView(btn_share,lp);
        //函数
        btn_share.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(mb_isSharing) {
                    unshare();
                }
                else{
                    share();
                }
            }
        });

    }

    public void unshare() {
        mb_isSharing = false;
        JsActivityContentDisplayManager.sharedInstance().undisplay(mMenuView);
    }

    void share() {
        mb_isSharing = true;
        {
            {
                //SlowLive
                if(mMenuView == null) {
                    mMenuView = new JsMenuView(MenuViewActivity.this);
                }
                JsDisplayStyle style = new JsDisplayStyle();
                RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,JsGeometryManager.pixelFromDp(200));
                lp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
                style.mRelativeLayoutParams_contentViewInitialLayoutParams = lp;
                style.mb_isMaskViewClickCancel = true;
                style.displayAnimationType = JsDisplayAnimationType.FromBottom;
                style.unDisplayAnimationType = JsUndisplayAnimationType.ToBottom;
                JsActivityContentDisplayManager.sharedInstance().display(MenuViewActivity.this, mMenuView, style, new JsDisplayCallback() {
                    @Override
                    public void onUndisplay(View contentView) {
                        mb_isSharing = false;
                    }
                });

                final int top = 100;
                final int itemViewRight_itemViewLeft = 30;
                final int parentLeft_itemViewRight = 60;

                mMenuView.mBasicCallback = new JsMenuViewBasicCallback() {
                    @Override
                    public Rect jsMenuViewSetItemOffset(JsMenuView fragment, int index) {
                        Rect rect = new Rect();
                        if(index == 0) {
                            rect.top = top;
                            rect.left = parentLeft_itemViewRight;
                            rect.right = itemViewRight_itemViewLeft;
                        }
                        else if(index == fragment.vms().size() - 1) {
                            rect.top = top;
                            rect.left = itemViewRight_itemViewLeft;
                            rect.right = itemViewRight_itemViewLeft;
                        }
                        else {
                            rect.top = top;
                            rect.left = itemViewRight_itemViewLeft;
                            rect.right = itemViewRight_itemViewLeft;
                        }
                        return rect;
                    }

                    @Override
                    public JsMenuViewAdapter jsMenuViewSetAdapter(JsMenuView menuView) {
                        return null;
                    }

                    @Override
                    public void jsMenuItemViewWillDisplay(RecyclerView.ViewHolder viewHolder, int position) {

                    }

                    @Override
                    public JsMenuViewVm jsMenuViewSetVm(JsMenuView menuView) {
                        JsMenuViewVm menuViewVm = new JsMenuViewVm();
                        menuViewVm.mi_layoutStyle = 0;
//                        menuViewVm.mi_borderWidth = 2;
//                        menuViewVm.mColor_background = Color.argb(255,200,200,200);
                        return menuViewVm;
                    }
                    @Override
                    public JsMenuItemView jsMenuViewCreateItemView(JsMenuView fragment, Context context) {
                        ShareItemView itemView = new ShareItemView(context);
                        return itemView;
                    }

                    @Override
                    public void jsMenuViewAfterCreateView(JsMenuView fragment) {

                    }

                    @Override
                    public ArrayList<JsMenuItemVm> jsMenuViewSetDatasource() {
                        ArrayList arrayList = new ArrayList();
                        int itemViewWidth = JsGeometryManager.pixelFromDp(60);
                        int itemViewHeight = JsGeometryManager.pixelFromDp(90);
                        {
                            ShareItemViewVm vm= new ShareItemViewVm("微信好友", R.mipmap.wxhy_share);
                            vm.getUnselectedStyle().mi_width = itemViewWidth;
                            vm.getUnselectedStyle().mi_height = itemViewHeight;
                            arrayList.add(vm);
                        }
                        {
                            ShareItemViewVm vm= new ShareItemViewVm("微信好友", R.mipmap.pyq_share);
                            vm.getUnselectedStyle().mi_width = itemViewWidth;
                            vm.getUnselectedStyle().mi_height = itemViewHeight;
                            arrayList.add(vm);
                        }
                        {
                            ShareItemViewVm vm= new ShareItemViewVm("QQ空间", R.mipmap.qqkj_share);
                            vm.getUnselectedStyle().mi_width = itemViewWidth;
                            vm.getUnselectedStyle().mi_height = itemViewHeight;
                            arrayList.add(vm);
                        }
                        {
                            ShareItemViewVm vm= new ShareItemViewVm("QQ好友", R.mipmap.qqhy_share);
                            vm.getUnselectedStyle().mi_width = itemViewWidth;
                            vm.getUnselectedStyle().mi_height = itemViewHeight;
                            arrayList.add(vm);
                        }
                        {
                            ShareItemViewVm vm= new ShareItemViewVm("微博", R.mipmap.wb_share);
                            vm.getUnselectedStyle().mi_width = itemViewWidth;
                            vm.getUnselectedStyle().mi_height = itemViewHeight;
                            arrayList.add(vm);
                        }
                        return arrayList;
                    }

                    @Override
                    public void jsMenuViewClickItem(JsMenuView fragment, int i, JsMenuItemVm model) {
                        JsLogManager.logc_d("MenuActivity/jsMenuViewClickItem","index is %d",i);
                    }
                };
            }
        }
    }

    public void updateUi(MenuActivityVm vm) {
        {
            //mv_slowLiveContainer
//            RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,mvm.mi_slowLiveViewHeight);
//            layoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
//            layoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
//            mv_slowLiveContainer.setLayoutParams(layoutParams);
//            mv_slowLiveContainer.setBackgroundColor(Color.argb(255,0,0,0));
        }

        {
            //MainMenu
            //mv_slowLiveContainer
//            RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,mvm.mi_mainMenuHeight);
//            layoutParams.addRule(RelativeLayout.BELOW,mv_slowLiveContainer.getId());
//            layoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
//            mv_mainMenuContainer.setLayoutParams(layoutParams);
//            mv_mainMenuContainer.setBackgroundColor(Color.argb(255,255,200,200));
        }
    }
}